import FileSaver from "file-saver";
import XLSX from "xlsx";

export function exportExcel(file_name = "fileName", id = "table_id") {

  
  // 获取当前日期和时间
  const now = new Date();

  // 获取年、月、日、时、分
  const year = now.getFullYear(); // 获取年份
  const month = String(now.getMonth() + 1).padStart(2, "0"); // 获取月份（注意月份从0开始，所以加1）
  const day = String(now.getDate()).padStart(2, "0"); // 获取日
  const hours = String(now.getHours()).padStart(2, "0"); // 获取小时
  const minutes = String(now.getMinutes()).padStart(2, "0"); // 获取分钟

  const formattedDateTime = `${year}${month}${day}${hours}${minutes}`;
  /* generate workbook object from table */
  id = "#" + id;
  file_name = file_name + "_" + formattedDateTime + ".xlsx";
  var xlsxParam = { raw: false }; // true会把当前内容当做字符串输出
  var wb = XLSX.utils.table_to_book(document.querySelector(id), xlsxParam);

  /* get binary string as output */
  var wbout = XLSX.write(wb, {
    bookType: "xlsx",
    bookSST: true,
    type: "array",
  });
  try {
    FileSaver.saveAs(
      new Blob([wbout], { type: "application/octet-stream" }),
      file_name
    );
  } catch (e) {
    if (typeof console !== "undefined") {
      console.log(e, wbout);
    }
  }
  return wbout;
}
